// pages/tabbar/classification/classification.js
import regeneratorRuntime from '../../../utils/runtime.js';
const classData = require('./classTest.js');
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  classificate
} from '../../../utils/imapi.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    show: 1,
    tagsList: [],
    categorys: '',
    data: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    shareFun.hidenShareMenu();
    const {data} = await classificate({})
    this.setData({
      height:app.globalData.screenHeight - app.globalData.CustomBar - app.globalData.screenWidth / 750 * 88 ,
      categorys: data,
    })
  },

  tabChange(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      currentTab: index
    })
  },

  search() {
    wx.navigateTo({
      url: '/pages/module/search/search',
    })
  },


  chooseItem(e) {
    var keyword = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/module/search/searchResult/searchResult?keyword=' + keyword
    })
  },

  startSearch(){
    var that = this;
    var brand_ids = '';
    var type_ids = '';
    that.data.tagsList.forEach(item => {
      if (item.type == 0) {
        brand_ids = brand_ids + item.id + ','
      }else{
        type_ids = type_ids + item.id + ','
      }
    });
    brand_ids = brand_ids.substr(0, brand_ids.length - 1);
    type_ids = type_ids.substr(0, type_ids.length - 1);

    wx.navigateTo({
      url: '/pages/module/search/searchResult/searchResult?keyword=' + '&type_ids=' + type_ids + '&brand_ids=' + brand_ids,
    })
  },


  // 删除tag
  deleteItem(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var obj = that.data.tagsList[index];
    that.data.tagsList.splice(index, 1);
    var current = obj.type;
    var id = obj.id;
    that.data.categorys[current].child.forEach((item, n) => {
      if (item.id == id) {
        item.is_selected = 0;
      }
    });
    that.setData({
      categorys: that.data.categorys,
      tagsList: that.data.tagsList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})